package com.qiqiim.webserver.user.service.impl;

import com.qiqiim.server.session.impl.SessionManagerImpl;
import com.qiqiim.webserver.user.dao.UserDepartmentDao;
import com.qiqiim.webserver.user.model.ImFriendUserData;
import com.qiqiim.webserver.user.model.ImFriendUserInfoData;
import com.qiqiim.webserver.user.model.UserDepartmentEntity;
import com.qiqiim.webserver.user.service.UserDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("userDepartmentServiceImpl")
public class UserDepartmentServiceImpl implements UserDepartmentService {
    @Autowired
    private UserDepartmentDao userDepartmentDao;
    @Autowired
    private SessionManagerImpl sessionManager;


    @Override
    public UserDepartmentEntity queryObject(Long id) {
        return userDepartmentDao.queryObject(id);
    }

    @Override
    public List<UserDepartmentEntity> queryList(Map<String, Object> map) {
        return userDepartmentDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return userDepartmentDao.queryTotal(map);
    }

    @Override
    public void save(UserDepartmentEntity userDepartment) {
        userDepartmentDao.save(userDepartment);
    }

    @Override
    public int update(UserDepartmentEntity userDepartment) {
        return userDepartmentDao.update(userDepartment);
    }

    @Override
    public int delete(Long id) {
        return userDepartmentDao.delete(id);
    }

    @Override
    public int deleteBatch(Long[] ids) {
        return userDepartmentDao.deleteBatch(ids);
    }

    @Override
    public List<ImFriendUserData> queryGroupAndUser(String userCode) {
        List<ImFriendUserData> friendgroup = userDepartmentDao.queryGroupAndUser();
        for (ImFriendUserData fg : friendgroup) {
            List<ImFriendUserInfoData> friends = fg.getList();
            List<ImFriendUserInfoData> editFriends = new ArrayList<>();
            if (friends != null && friends.size() > 0) {
                for (ImFriendUserInfoData fr : friends) {
                    if (userCode.length() == 20 && fr.getCode().equals(getRealParent(userCode))) {
                        editFriends.add(fr);
                    } else {

                    }
                    if (userCode.length() != 20 && fr.getCode().startsWith(userCode) && fr.getCode().length() == 20) {
                        editFriends.add(fr);
                    } else {

                    }
                    boolean exist = sessionManager.exist(fr.getId().toString());
                    if (exist)
                        fr.setStatus("online");
                }
            }
            fg.setList(editFriends);
        }
        return friendgroup;
    }

    String getRealParent(String code) {
        StringBuffer buffer = new StringBuffer();
        code = code.substring(0, code.length() - 4);
        while (code.endsWith("0000")) {
            code = code.substring(0, code.length() - 4);
        }
        return code;
    }

}
